package com.act.moudles.baseAct.service.impl;

import com.act.components.UserComponent;
import com.act.config.VarsTypeEnums;
import com.act.constant.LoginTypeEnums;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.act.moudles.baseAct.dao.TaskAnoVarsDao;
import com.act.moudles.baseAct.entity.TaskAnoVars;
import com.act.moudles.baseAct.service.TaskAnoVarsService;
import javafx.concurrent.Task;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 任务变量设置(TaskAnoVars)表服务实现类
 *
 * @author makejava
 * @since 2023-05-19 15:58:45
 */
@Service("taskAnoVarsService")
public class TaskAnoVarsServiceImpl extends ServiceImpl<TaskAnoVarsDao, TaskAnoVars> implements TaskAnoVarsService {

    @Resource
    private UserComponent userComponent;

    @Override
    public String addVars(TaskAnoVars taskAnoVars) {
        String checkRes = checkUnion(taskAnoVars);
        if(StringUtils.isNotEmpty(checkRes)){
            return checkRes;
        }
        //默认可以编辑
        taskAnoVars.setVarType(VarsTypeEnums.CUR_VARS.getVarType());
        taskAnoVars.setCanEdit(1);
        taskAnoVars.setCreateBy(userComponent.getCurrentUser(LoginTypeEnums.WEB.getType()).getRealName());
        taskAnoVars.setCreateTime(new Date());
        save(taskAnoVars);
        return null;
    }

    @Override
    public String updVars(TaskAnoVars taskAnoVars) {
        String checkRes = checkUnion(taskAnoVars);
        if(StringUtils.isNotEmpty(checkRes)){
            return checkRes;
        }
        updateById(taskAnoVars);
        return null;
    }




    /**
     * 查询重复数据
     * @param taskAnoVars
     * @return
     */
    private String checkUnion(TaskAnoVars taskAnoVars){
        LambdaQueryWrapper<TaskAnoVars> query = new LambdaQueryWrapper<>();
        query.ne(taskAnoVars.getVarId() != null,TaskAnoVars::getVarId,taskAnoVars.getVarId());
        query.eq(TaskAnoVars::getAnoId,taskAnoVars.getAnoId());
        query.eq(TaskAnoVars::getVarKey,taskAnoVars.getVarKey());
        long counts = count(query);
        if(counts > 0L){
            return "该节点变量已经存在";
        }
        return null;
    }
}

